package com.uam.web.common.thread;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.db.model.entity.RoutQueryModel;
import com.uam.utils.ZipUtil;
import com.uam.web.common.utils.RouterUtils;
import com.uam.web.common.vo.*;
import com.uam.web.controller.RouteController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * 线程启用接口
 *
 * @author heguanglong
 * @date 2023/6/13
 */
@Slf4j
public class RouteTop20Thread implements Runnable {

    private CountDownLatch countDownLatch;
    private JSONArray data;
    private Map<Long, RoutQueryModel> taskMap;
    private RouteController controller;
    private List<RouteInfoVo> routeVoList;

    public RouteTop20Thread(CountDownLatch countDownLatch, JSONArray data,
                            Map<Long, RoutQueryModel> taskMap,
                            RouteController controller,
                            List<RouteInfoVo> routeVoList) {
        this.countDownLatch = countDownLatch;
        this.data = data;
        this.taskMap = taskMap;
        this.controller = controller;
        this.routeVoList = routeVoList;


    }

    @Override
    public void run() {
        try {
            Long id = data.getJSONObject(0).getLong("task_id");
            RoutQueryModel r = taskMap.get(id);
            AircraftVo aircraftVo = null;
            TaskVo taskVo = null;
            if (r != null) {
                aircraftVo = new AircraftVo();
                BeanUtils.copyProperties(r, aircraftVo);
                taskVo = new TaskVo(r.getTaskId(), r.getTaskName(), r.getStartTime(), r.getEndTime(), JSONArray.parseArray(r.getSettings(), SiteVo.class), r.getStatus(),r.getSignalType());
            }
            RouteTrackVo trackVo= RouterUtils.getRouteTrackVo(data,r,id,taskVo);

            if(r !=null && StringUtils.isNotBlank(r.getTrackPoints())){
                JSONArray jsonArray =JSONArray.parseArray(ZipUtil.uncompressBase64(r.getTrackPoints()));
                trackVo.setTrackInfo(jsonArray);
            }
            RouteInfoVo routeInfoVo = new RouteInfoVo(aircraftVo, taskVo, trackVo);
            RouteTrackVo trackVo1= routeInfoVo.getRouteTrackVo();
            JSONArray outJson=new JSONArray(4);
            List<PointVo>  listPointVo=  trackVo1.getPointVo();

            if(listPointVo != null){
                JSONObject jsonObject=new JSONObject(2);
                JSONArray jsonArrayTemp1=new JSONArray(1034);
                listPointVo.forEach(a->{
                    JSONArray jsonArray=new JSONArray(2);
                    jsonArray.add(a.getPoints().get(0));
                    jsonArray.add(a.getPoints().get(1));
                    jsonArrayTemp1.add(jsonArray);
                });
                jsonObject.put("lnglat",jsonArrayTemp1);
                outJson.add(jsonObject);

            }

            JSONObject jsonObject=new JSONObject(2);
            jsonObject.put("lnglat",trackVo1.getTrackInfo());
            outJson.add(jsonObject);

//            log.info("返回id：{}   josn：{}",id,JSONArray.toJSONString(outJson));
            routeVoList.add(routeInfoVo);
        } catch (Exception e) {
            log.error("top20 数据解析错误 ", e);
        }
        countDownLatch.countDown();
    }

}

